package cn.gailvlun.gll.presentation.hotel.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorRes;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.util.DensityUtil;



public class DividerDecoration extends RecyclerView.ItemDecoration {
    private Paint paint;
    private int space;

    public DividerDecoration(Context context) {
        this(context, 1, R.color.white);
    }

    public DividerDecoration(Context context, @ColorRes int colorRes) {
        this(context, 1, colorRes);
    }

    public DividerDecoration(Context context, int height, @ColorRes int colorRes) {
        space = DensityUtil.dp2px(context, height);
        paint = new Paint();
        paint.setColor(context.getResources().getColor(colorRes));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = space;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int space = DensityUtil.dp2px(parent.getContext(), 1);
        for (int i = 0; i < childCount - 1; i++) {
            View view = parent.getChildAt(i);

            float top = view.getBottom();
            float bottom = view.getBottom() + space;
            c.drawRect(left, top, right, bottom, paint);

        }
    }
}
